package com.dhh.sky.util;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.HashMap;

public class SongsManager {
	// SDCard Path
	final String MEDIA_PATH = new String("/sdcard/skydrive");
	private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
	
	// Constructor
	public SongsManager(){
		
	}
	
	public ArrayList<HashMap<String, String>> getPlayList(){
		File home = new File(MEDIA_PATH);

		if (home.listFiles(new FileExtensionFilter()).length > 0) {
			for (File file : home.listFiles(new FileExtensionFilter())) {
				HashMap<String, String> song = new HashMap<String, String>();
				song.put(Constant.SONG_TITLE, file.getName());
				song.put(Constant.SONG_PATH, file.getPath());
				
				// Adding each song to SongList
				songsList.add(song);
			}
		}
		// return songs list array
		return songsList;
	}
	
	public int getSongIndex(String path){
		int index = 0;
		
		if(path!=null && path.length()>0){
			for(int i=0; i<songsList.size(); i++){
				HashMap<String,String> map = songsList.get(i);
				String songPath = map.get(Constant.SONG_PATH);
				if(path.toLowerCase().contains(songPath.toLowerCase())){
					index = i;
					break;
				}
			}
		}		
		
		return index;
	}
	
	/**
	 * Class to filter files which are having .mp3 extension
	 * */
	class FileExtensionFilter implements FilenameFilter {
		public boolean accept(File dir, String name) {
			String ext = CUtil.getFileExtesion(name);
			boolean flag = false;
			if(ext!=null){
				flag = Constant.musicFileType.contains(ext.toLowerCase());
			}
			return flag;
		}
	}
}
